package com.yayan.beautyclub.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yayan.beautyclub.bean.Reserve;
import com.yayan.beautyclub.service.IReserveService;
import com.yayan.util.EncodingUtil;

@Controller
@RequestMapping(value="/reserve", method=RequestMethod.POST)
public class ReserveController {
	@Autowired
	private IReserveService reserveService;
	
	/**
	 * 后台管理获取预约信息分页列表
	 * @param page
	 * @param size
	 * @param first
	 * @param second
	 * @param search
	 * @return
	 */
	@RequestMapping(value="/reserve.do")
	@ResponseBody
	public List<Map<String, Object>> reservePage(String page, String size, String first, String second, String search){		
		search = EncodingUtil.encodeStr(search);		
		int intPage = Integer.parseInt(page);
		int intSize = Integer.parseInt(size);
		int intFirst = Integer.parseInt(first);
		int intSecond = Integer.parseInt(second);
		List<Map<String, Object>> list = reserveService.findReserveByCondition(intPage, intSize, intFirst, intSecond, search);		
		return list;
	}
	
	/**
	 * 用户创建预约订单
	 * @param reserve
	 * @return
	 */
	@RequestMapping(value="/create.do")
	@ResponseBody
	public boolean createReserve(@RequestBody Reserve reserve){		
		if(reserve.getName() != null && reserve.getPhone() != null && reserve.getuId() >= 0
				&& reserve.getpId() > 0 && reserveService.saveReserve(reserve) != 0L)
			return true;
		return false;
	}
	
	/**
	 * 用户根据用户id获取预约信息
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/reserve_list.do")
	@ResponseBody
	public List<Map<String, Object>> getReserveList(@RequestBody Map<String, String> map){
		long uId = Long.parseLong(map.get("uId"));
		
		return reserveService.findReserveByUserId(uId, -1, 0, 0);
	}
	
	@RequestMapping(value="/delete_reserve.do")
	@ResponseBody
	public boolean deleteReserve(@RequestBody Map<String, String> map){
		long rId = Long.parseLong(map.get("rId"));		
		return reserveService.cancelReserve(rId);
	}
}
